import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JButton;
import javax.swing.JComponent;
//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGEncodeParam;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class SwingCapture extends Panel implements ActionListener {
	
	public static Player player = null;
	public CaptureDeviceInfo di = null;
	public MediaLocator ml = null;
	public JButton capture = null;
	public Buffer buf = null;
	public Image img = null;
	public VideoFormat vf = null;
	public BufferToImage btoi = null;
	public ImagePanel imgpanel = null;
	 

	public SwingCapture()
	{
		setLayout(new BorderLayout());
		setSize(640, 480);
		imgpanel = new ImagePanel();
		capture = new JButton("Capture");
		capture.addActionListener(this);
		
		String str1 = "vfw:Logitech USB Video Camera:0";
		String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
		String str3 = "rtsp://192.168.178.50:554/ipcam.sdp";
		
		//java.util.Vector deviceListVector = CaptureDeviceManager.getDeviceList(null);
		
		//di = CaptureDeviceManager.getDevice(str2); // Getting Camera Device Information
		//System.out.println("hjw: instance capture device started !! " + di);
		//ml = di.getLocator(); // MediaLocator for Camera
		//System.out.println("hjw: instance getLocator started !! " + di);
			
		try {
			ml = new MediaLocator(str2);
			player = Manager.createRealizedPlayer(ml); // Creating Player
			//player = Manager.createRealizedPlayer(ml); // Creating Player
			player.start(); // Start Camera Player
			Component comp; // for Getting Visual Player Component of Camera
			if ((comp = player.getVisualComponent()) != null) {
				add(comp, BorderLayout.NORTH); // Add Camera to North
			}
			add(capture, BorderLayout.CENTER); // Add Capture Button in Between
			add(imgpanel, BorderLayout.SOUTH); // Add Captured image
		    }

		catch (Exception e) {
		  e.printStackTrace();
		  System.out.println("hjw1");
		}
	}

	public static void main(String[] args) 
	{
		Frame f = new Frame("SwingCapture"); 				// Creating New Frame
		SwingCapture cf = new SwingCapture(); 				// Main Object Create
		f.addWindowListener(
				new WindowAdapter(){  						// Window Closing
						public void windowClosing(WindowEvent e) {
							playerclose(); 					// Closing Player
							System.exit(0);
						}
				}
			);
		f.add("Center", cf);								 // Adding Main Object to Frame 
		f.pack(); 											 //Packing Frame
		f.setSize(new Dimension(640, 480));
		f.setVisible(true);
	}
	
	public static void playerclose() // Function While Closing Player
	{
		player.close();
		player.deallocate();
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		JComponent c = (JComponent) e.getSource();// Getting Source of Event
		System.out.println("hjw1: c created");
		
		if (c == capture) // Capture Button Event
		{
			FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
			System.out.println("hjw1: fgc created");
			
			buf = fgc.grabFrame(); 									// Grabbing a frame
			btoi = new BufferToImage((VideoFormat) buf.getFormat());// Convert it to an image  Buffer
			img = btoi.createImage(buf); 							// Creating Image from Buffer
			imgpanel.setImage(img);            						// show the image
			saveJPG(img, "c:\\test.jpg"); 							// save image Function
			System.out.println("hjw: saved!!");
		}

	}

	class ImagePanel extends Panel 		// Panel for Displaying Captured Image
	{
		public Image myimg = null;

		public ImagePanel() {
			setLayout(null);
			setSize(320, 240);
		}

		public void setImage(Image img) {
			this.myimg = img;
			// }
			// }
			repaint();
		}

		public void paint(Graphics g) {
			if (myimg != null) {
				g.drawImage(myimg, 0, 0, this);
			}

		}
	}
	
	public static void saveJPG(Image img, String s) 
	{
		BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
		// }
		Graphics2D g2 = bi.createGraphics();
		g2.drawImage(img, null, null);
		FileOutputStream out = null;
		try{
			out = new FileOutputStream(s);
		} 
		catch (java.io.FileNotFoundException io) {
			System.out.println("File Not Found");
		}
		// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		// JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
		// param.setQuality(0.5f,false);
		// encoder.setJPEGEncodeParam(param);
		try {// encoder.encode(bi);
			out.close();
			} 
		catch (java.io.IOException io) {
			System.out.println("IOException");
			System.out.println("hjw2");
			}
	}

}
